<?php

/**
 * Class Xu ly quyen NSD
 */
class System_PermissionController extends Zend_Controller_Action {

    public function init() {
        //Load cau hinh thu muc trong file config.ini
        $app = Zend_Registry::get('App');
        //Lay duong dan thu muc goc (path directory root)
        $this->view->baseUrl = $baseUrl = $this->_request->getBaseUrl() . "/public/";
        $result = array(
                    array($baseUrl.'lib-js/system/admin/System_Admin_Permission.js','js')
                    ,array($baseUrl.'lib-js/system/jsTree/_lib/jquery.cookie.js','js')
                    ,array($baseUrl.'lib-js/system/jsTree/_lib/jquery.hotkeys.js','js')
                    ,array($baseUrl.'lib-js/system/jsTree/jquery.jstree.js','js')
                );                  


        if ($this->_request->isXmlHttpRequest()){
            Zend_Layout::startMvc(array(
                'layoutPath' => $app->layout,
                'layout' => 'partial'
            ));
            $this->view->arrJsCss = Zend_Json::encode($result);
        }else{
            //Cau hinh cho Zend_layout
            Zend_Layout::startMvc(array(
                'layoutPath' => $app->layout,
                'layout' => 'index'
            ));
            //Load ca thanh phan cau vao trang layout (index.phtml)
            $response = $this->getResponse();
            //Dinh nghia current modul code
            $this->view->currentModulCode = "SYSTEM";
            $this->view->currentModulCodeForLeft = "permission";
            $this->view->postHeader = Clound_String::registerFile($result);
            //Hien thi file template
            $response->insert('menu', $this->view->renderLayout('menu.phtml', './application/layout/scripts/'));     //Hien thi header 
            $response->insert('submenu', $this->view->renderLayout('submenu.phtml', './application/layout/scripts/'));     //Hien thi header 
            $response->insert('footer', $this->view->renderLayout('footer.phtml', './application/layout/scripts/'));   //Hien thi f
        }
    }
   
    // Xu ly thong tin de day ra file index.phtml
    public function indexAction() {
        $objListType = new System_Models_Listtype();
        $oCacheFile = new Clound_CacheFile();
        $arrPerType = $oCacheFile->getList('DM16');
        $this->view->arrPerType = $arrPerType;
    }
    public function dataAction(){
        $model = new System_Models_Permission();
        $parames = $this->_request->getParams();
        $data = $model->getAll($parames);
        echo Zend_Json::encode($data);
        die;
    }

    public function loadtreeAction(){
        $Unit = new System_Models_Unit();
        $Staff = new System_Models_Staff();
        $arrTree = '';
        $idNode = $this->_request->getParam('id','');

        if(!isset($_SESSION['TREE'])){
            $_SESSION['TREE'] = $Unit->getAllTree();
        }
        if(!isset($_SESSION['staff'])){
            $_SESSION['staff'] = $Staff->getAllTree();
        }
        $arrTreeGetAll = $_SESSION['TREE'];
        $arrTree = $this->getChildNode($idNode,$arrTreeGetAll);
        echo json_encode($arrTree);
        die;
    }
    
    public function getChildNode($idNode,$arrTreeGetAll){
        $arrOutput = array();
        if($idNode==''){
            $rel ='home';
        }else{
            $rel ='folder7';
        }
        foreach ($arrTreeGetAll as $key => $value) {
            if($value['UnitParent'] == $idNode){
                array_push($arrOutput, array('attr'=>array(
                                                'id'=>$value['UnitID']
                                                ,'rel'=>$rel
                                                ,'class'=>'no_checkbox'
                                            ),
                                            'data'=>$value['Name'],
                                            'state'=>'closed'
                ));
            }
        }
        foreach ($_SESSION['staff'] as $key => $value) {
            if($value['UnitID'] == $idNode){
                array_push($arrOutput, array('attr'=>array(
                                                'id'=>$value['StaffID']
                                                ,'rel'=>'user'
                                                ,'class'=>'staff'
                                            ),
                                            'data'=>$value['Name'],
                                            // 'state'=>'closed'
                ));
            }
        }
        return $arrOutput;
    }

    public function rolesAction(){
        $StaffIDList = $this->_request->getParam('StaffIDList');
        $model = new System_Models_Permission();
        $parames = $this->_request->getParams();
        $data = $model->getAll($StaffIDList);
        $aOutput = array();
        echo Zend_Json::encode($data);
        die;
    }
    public function getpermisionAction(){
        $StaffIDList = $this->_request->getParam('StaffIDList');
        $model = new System_Models_Permission();
        $data = $model->getPermission($StaffIDList);
        echo Zend_Json::encode($data);
        die;
    }
    public function saverolesAction(){
        $StaffIDList = $this->_request->getParam('StaffIDList');
        $model = new System_Models_Permission();
        $permission = $this->_request->getParam('permission');
        $pdefault = $this->_request->getParam('pdefault');
        $aStaffID = explode(',', $StaffIDList);
        foreach ($aStaffID as $key => $StaffID) {
            // Delete permission old
            $model->delete("StaffID='$StaffID'");

            $iMax = sizeof($permission);
            for ($i=0; $i < $iMax; $i++) { 
                $arrInput = array(
                                    'StaffID'=>$StaffID
                                    ,'Roles'=>$permission[$i]
                                    ,'DefaultSet'=>$pdefault[$i]
                                );
                $model->insert($arrInput);
            }
        }
        die;
    }

    public function refreshAction(){
        $modelLogin = new Models_Login();
        $modelLogin->setSession();
        die;
    }
}

?>